//
//  WHMatchingStrTool.m
//  AddressBook
//
//  Created by  on 2020/7/30.
//  Copyright © 2020 wh. All rights reserved.
//

#import "WHMatchingStrTool.h"

@implementation WHMatchingStrTool
/**
 得到符合条件的数组数据

 @param arr 数组数据
 @param str 条件字符串。 用户输入的字符串
 @return 符合条件的数组
 */
+ (NSArray *)getIncludeConditionArr:(NSArray *)arr conditionStr:(NSString *) str
{
    NSString *lowerStr = [str lowercaseString];
    NSArray *tempArrChineseStr = arr;//包含汉字的字符串
    NSArray<NSArray *> *tempArrStr = (NSArray<NSArray *> *)[self converToStrArr:arr];//没有汉字的字符串
    NSMutableArray *newArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < tempArrStr.count; i++) {
        unsigned long range = [[tempArrChineseStr[i] lowercaseString] rangeOfString:lowerStr].length;
        if (range > 0) {
            [newArr addObject:tempArrChineseStr[i]];
        }else{
            NSArray<NSString *> *nameArr = tempArrStr[i];
            if ([self secondCompareWithuncertainStrArr:nameArr inputStr:lowerStr]) {
                [newArr addObject:tempArrChineseStr[i]];
            }
        }
    }
    return newArr;
    
}


/**
 短字符串与长字符串判断是否符合短字符串与长字符串的前部分相同的条件
 @param shortStr 短字符串
 @param longStr 长字符串
 @return YES符合条件 NO 不符合
 */
+ (BOOL)compareWithShortStr:(NSString *)shortStr longStr:(NSString *)longStr
{
    NSRange range = [longStr rangeOfString:shortStr];
    unsigned long rangeLocation = range.location;
    unsigned long rangeLength = range.length;
    if (rangeLocation == 0 && rangeLength > 0) {
        return YES;
    }else{
        return NO;
    }
}


/**
 将输入的字符串与本地的数组数据相比较
 @param uncertainStrArr 要比较的数组
 @param inPutStr 输入的字符串
 @return YES符合条件
 */
+ (BOOL)secondCompareWithuncertainStrArr:(NSArray<NSString *> *)uncertainStrArr inputStr:(NSString *)inPutStr
{
    BOOL isConform = NO;//最后的结果
    NSString *nameStr = @"";
    NSMutableArray *nameStrArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < uncertainStrArr.count; i++) {
        nameStr = [nameStr stringByAppendingString: uncertainStrArr[i]];
        [nameStrArr addObject:@(uncertainStrArr[i].length)];
        if (uncertainStrArr[i].length >= inPutStr.length) {
            if (!isConform) {
                isConform = [ self compareWithShortStr:inPutStr longStr:uncertainStrArr[i]];
            }
        }
    }
    if (isConform) {
        return isConform;
    }
    for (int i = 0; i < uncertainStrArr.count; i++) {
        if (nameStr.length > inPutStr.length) {
            isConform = [ self compareWithShortStr:inPutStr longStr:nameStr];
            if (isConform) {
                return isConform;
            }
            int index = [nameStrArr[i] intValue];
            nameStr = [nameStr substringFromIndex:index];
        }else{
            break;
        }
    }
    return isConform;
}


/**
 将有汉字的数组转换为没有汉字的数组

 @param chineseStrArr 有汉字的数组
 @return 没有汉字的数组
 */
+ (NSArray *)converToStrArr:(NSArray *)chineseStrArr
{
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < chineseStrArr.count; i++) {
        NSString *str = [self convertToIntervalStrWithChineseCharactor:chineseStrArr[i]];
        NSArray *everNameArr = [str componentsSeparatedByString:@"&&"];
        [tempArr addObject:everNameArr];
    }
    return tempArr;
    
}


/**
 将带有汉字的字符串转为拼音字符串

 @param charactor <#charactor description#>
 @return <#return value description#>
 */
+ (NSString *)convertToIntervalStrWithChineseCharactor:(NSString *)charactor {
    //1.将字符串转变为可变
    NSMutableString *charactorM = [NSMutableString stringWithString:charactor];
    //2.将可变字符串转变为带声调的拼音
    Boolean success = CFStringTransform((CFMutableStringRef)charactorM, NULL, kCFStringTransformMandarinLatin, false);
    if (success == false) {
        NSLog(@"转换出错 -- 1");
        return nil;
    }
    //3.去掉声调
    Boolean successT = CFStringTransform((CFMutableStringRef)charactorM, NULL, kCFStringTransformStripDiacritics, NO);
    
    if (successT == false) {
        NSLog(@"转换出错 -- 2");
        return nil;
    }
    //去掉换行 将每个字转换的拼音以 && 分割开
    NSString *ca = [charactorM stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    ca = [ca stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    ca = [ca stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    ca = [ca stringByReplacingOccurrencesOfString:@" " withString:@"&&"];
    return ca;
}

/**
 将带有汉字的字符串转为拼音字符串
 
 @param charactor <#charactor description#>
 @return <#return value description#>
 */

+ (NSString *)convertToStrWithChineseCharactor:(NSString *)charactor {
    //1.将字符串转变为可变
    NSMutableString *charactorM = [NSMutableString stringWithString:charactor];
    //2.将可变字符串转变为带声调的拼音
    Boolean success = CFStringTransform((CFMutableStringRef)charactorM, NULL, kCFStringTransformMandarinLatin, false);
    if (success == false) {
        NSLog(@"转换出错 -- 1");
        return nil;
    }
    //3.去掉声调
    Boolean successT = CFStringTransform((CFMutableStringRef)charactorM, NULL, kCFStringTransformStripDiacritics, NO);
    
    if (successT == false) {
        NSLog(@"转换出错 -- 2");
        return nil;
    }
    //去掉换行 将每个字转换的拼音以 && 分割开
    NSString *ca = [charactorM stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    ca = [ca stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    ca = [ca stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    ca = [ca stringByReplacingOccurrencesOfString:@" " withString:@""];
    return ca;
}

@end
